package com.umeox.watch.moto.dataservice.mqtt.rpc;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.text.TextUtils;

import com.alibaba.fastjson.JSONObject;
import com.umeox.moto.common.log.Logger;
import com.umeox.watch.moto.dataservice.base.BaseApp;
import com.umeox.watch.moto.dataservice.constants.Constants;
import com.umeox.watch.moto.dataservice.constants.MqttConstants;
import com.umeox.watch.moto.dataservice.services.LocationService;


/**
 * Created by Rambo 2020/5/7
 * Describe:
 */
public class EventRpc extends BaseRpc {

    private static final int STATUS_CALLING = 1;
    private static final int STATUS_CANCEL_CALLING = 2;
    private static final int TYPE_VIDEO_CALL = 1;
    private static final int TYPE_VOICE_CALL = 2;
    private static final int TYPE_PTT_CALL = 3;

    private EventRpc(Builder builder) {
        this.params = builder.params;
        this.method = builder.method;
    }

    @SuppressLint("WrongConstant")
    @Override
    public void handleOrResponse() {
        if (isRequestLocation()) {
            if (params != null) {
                String cmdId = params.getString("reqId");
                int locPriority = params.getIntValue("locPriority");
                Logger.d("远程定位请求>>>reqId:" + cmdId + ",locPriority=" + locPriority);
                LocationService.cmdReqLocation(BaseApp.getInstance(), cmdId, locPriority);
            }
        } else if (isRequestVoipCall()) {
//            PowerManager mPowerManager = (PowerManager) BaseApp.getInstance().getSystemService(Context.POWER_SERVICE);
//            PowerManager.WakeLock mWakeLock = mPowerManager.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.SCREEN_DIM_WAKE_LOCK, "My:Tag");
//            mWakeLock.acquire();

            //服务器请求视频通话
            String channel = params.getString("channel");
            String fuid = params.getString("fromMemberId");
            int status = params.getIntValue("status");
            int type = params.getIntValue("type");
            if (status == STATUS_CALLING) {
                if (type == TYPE_VIDEO_CALL) {//请求视频呼叫
                    Logger.d("收到视频通话指令");
                    //声网视频电话呼叫
                    Logger.i("Send Broadcast[" + Constants.ACTION_INCOMING_VIDEO_CALL + "]");
                    Intent intent = new Intent(Constants.ACTION_INCOMING_VIDEO_CALL);
                    intent.putExtra("channel", channel);
                    intent.putExtra("fuid", fuid);
                    intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES | Intent.FLAG_RECEIVER_FOREGROUND | Constants.FLAG_RECEIVER_INCLUDE_BACKGROUND);
                    BaseApp.getInstance().sendBroadcast(intent);
                } else if (type == TYPE_VOICE_CALL) {//请求语音呼叫
                    Logger.d("收到语音通话指令");
                    //声网视频电话呼叫
                    Logger.i("Send Broadcast[" + Constants.ACTION_INCOMING_VOIP_CALL + "]");
                    Intent intent = new Intent(Constants.ACTION_INCOMING_VOIP_CALL);
                    intent.putExtra("channel", channel);
                    intent.putExtra("fuid", fuid);
                    intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES | Intent.FLAG_RECEIVER_FOREGROUND | Constants.FLAG_RECEIVER_INCLUDE_BACKGROUND);
                    BaseApp.getInstance().sendBroadcast(intent);
                } else if(type == TYPE_PTT_CALL){ //PTT
                    Logger.d("收到PTT通话指令");
                    //声网视频电话呼叫
                    Logger.i("Send Broadcast[" + Constants.ACTION_INCOMING_PTT_CALL + "]");
                    Intent intent = new Intent(Constants.ACTION_INCOMING_PTT_CALL);
                    intent.putExtra("channel", channel);
                    intent.putExtra("fuid", fuid);
                    intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES | Intent.FLAG_RECEIVER_FOREGROUND | Constants.FLAG_RECEIVER_INCLUDE_BACKGROUND);
                    BaseApp.getInstance().sendBroadcast(intent);
                }
            }

//            if (mWakeLock != null) {
//                mWakeLock.release();
//            }
        }
    }


    public static class Builder {
        private JSONObject params;
        private String method;

        public Builder params(JSONObject params) {
            this.params = params;
            return this;
        }

        public Builder method(String method) {
            this.method = method;
            return this;
        }


        public EventRpc build() {
            return new EventRpc(this);
        }

    }

    private boolean isRequestLocation() {
        return !TextUtils.isEmpty(this.method) && method.equals(MqttConstants.ServerRpcMethod.REQ_LOC);
    }

    private boolean isRequestVoipCall() {
        return !TextUtils.isEmpty(this.method) && method.equals(MqttConstants.ServerRpcMethod.REQ_VOIP_CALL);
    }
}
